package com.atm.servlet;

import com.atm.pojo.User;
import com.atm.service.UserService;
import com.atm.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * 修改用户个人密码
 */
@WebServlet("/UserChange")
public class UserChangeServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得密码
        String icno = req.getParameter("icno");
        String oldpwd = req.getParameter("oldpwd");
        String newpwd = req.getParameter("newpwd");
        String newpwd2 = req.getParameter("newpwd2");
        //检验密码
        User userByNameAndPwd = userService.findUserByNameAndPwd(icno, oldpwd);
        if (userByNameAndPwd == null){
            //密码错误
            req.setAttribute("error1","密码有误");
            req.getRequestDispatcher("/changepage/UserChangePwd.jsp").forward(req,resp);
        }else {
            if (newpwd != null && newpwd.equals(newpwd2)){
                boolean result=userService.updateUserPwd(icno,newpwd);
                if (result){
                    req.getRequestDispatcher("/changepage/login.jsp").forward(req,resp);
                }else {
                    req.setAttribute("error1","更新失败");
                    req.getRequestDispatcher("/changepage/UserChangePwd.jsp").forward(req,resp);
                }
            }else {
                req.setAttribute("error1","输入密码不一致");
                req.getRequestDispatcher("/changepage/UserChangePwd.jsp").forward(req,resp);
            }
        }
        //比较内容

    }
}
